<template>
  <div id="user-view">
    <div class="top">
      <div class="operation">
        <div class="button" @click="showAddPage">
          <div class="button-icon">
            <Icon type="md-add" />
          </div>
          <div class="button-text">新增</div>
        </div>
      </div>
      <div class="search">
        <div class="search-item">
          <div class="item-name">用户名：</div>
          <div class="item-value">
            <Input
              clearable
              v-model="search.username"
              suffix="ios-search"
              placeholder="请输入用户名"
              style="width: auto"
            />
          </div>
        </div>
        <Button
          type="primary"
          shape="circle"
          icon="ios-search"
          @click="pageSelect"
          >搜索
        </Button>
      </div>
    </div>
    <div class="content">
      <div class="table-title">
        <div class="title-item id">
          <div class="item-name">序号</div>
        </div>
        <div class="title-item">
          <div class="item-name">用户名</div>
        </div>
        <div class="title-item">
          <div class="item-name">昵称</div>
        </div>
        <div class="title-item">
          <div class="item-name">角色</div>
        </div>
        <div class="title-item status">
          <div class="item-name">状态</div>
        </div>
        <div class="title-item button">
          <div class="item-name">操作</div>
        </div>
      </div>
      <div class="table-content-container">
        <div class="table-content">
          <div class="line" v-for="(item, index) in userList" :key="index">
            <div class="cell id">{{ index + 1 }}</div>
            <div class="cell">{{ item.username }}</div>
            <div class="cell">{{ item.realName }}</div>
            <div class="cell roles">
              <div class="role" v-for="(role, n) in item.roleList" :key="n">
                {{ getRoleName(role) }}
              </div>
            </div>
            <div class="cell status">
              <Switch v-model="item.enabled" disabled size="large">
                <template #open>
                  <span>启用</span>
                </template>
                <template #close>
                  <span>停用</span>
                </template>
              </Switch>
            </div>
            <div class="cell buttons">
              <div class="button" @click="showUpdatePage(item)">编辑</div>
              <div class="button remove" @click="showRemovePage(item)">
                删除
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
    <div class="footer">
      <Page
        :total="page.total"
        :page-size="page.size"
        :model-value="page.current"
        show-sizer
        show-total
        :page-size-opts="[10, 15, 20, 50]"
        @on-change="changePage"
        @on-page-size-change="changePageSize"
      />
    </div>
    <div class="more-page" v-if="showMorePage">
      <div class="page">
        <div class="page-top">
          <div class="top-left">{{ pageTitle }}</div>
          <div class="button" @click="closePage">
            <Icon type="md-close" />
          </div>
        </div>
        <div class="page-content">
          <AddUser v-if="pageIndex === 0" :user="user"></AddUser>
          <UpdateUser v-if="pageIndex === 1" :user="user"></UpdateUser>
          <DeleteUser v-if="pageIndex === 2" :user="user"></DeleteUser>
        </div>
        <div class="page-footer">
          <div class="button" @click="submit">确定</div>
          <div class="button">取消</div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import AddUser from "@/components/user/AddUser";
import UpdateUser from "@/components/user/UpdateUser";
import { add, getAll, remove, update } from "@/api/user/user";
import { getAllRole } from "@/api/role/role";
import { Message } from "view-ui-plus";
import DeleteUser from "@/components/user/DeleteUser";

export default {
  name: "UserView",
  components: { AddUser, UpdateUser, DeleteUser },
  data() {
    return {
      page: {
        total: 0,
        current: 1,
        size: 15,
      },
      search: {
        username: null,
        enabled: null,
      },
      showMorePage: false,
      pageIndex: 0,
      pageTitle: "",
      user: {
        id: 0,
        username: null,
        realName: null,
        roles: [],
        password: null,
        enabled: false,
      },
      roleList: [],
      userList: [],
    };
  },
  created() {
    this.getRoleList();
    this.pageSelect();
  },
  methods: {
    getRoleList() {
      getAllRole().then((res) => {
        this.roleList = res.data;
      });
    },
    pageSelect() {
      this.search.page = this.page;
      getAll(this.search).then((res) => {
        if (res.code === 200) {
          this.userList = res.data.records;
          this.page.total = res.data.total;
        }
      });
    },
    closePage() {
      this.user = {};
      this.showMorePage = false;
    },
    showAddPage() {
      this.pageTitle = "新增用户";
      this.pageIndex = 0;
      this.showMorePage = true;
    },
    showUpdatePage(user) {
      this.user = user;
      this.pageTitle = "编辑用户";
      this.pageIndex = 1;
      this.showMorePage = true;
    },
    showRemovePage(user) {
      this.user = user;
      this.pageTitle = "删除用户";
      this.pageIndex = 2;
      this.showMorePage = true;
    },
    submit() {
      console.log(this.user);
      if (this.pageIndex == 0) {
        add(this.user).then((res) => {
          Message.success(res.msg);
          this.closePage();
          this.pageSelect();
        });
      } else if (this.pageIndex == 1) {
        update(this.user).then((res) => {
          Message.success(res.msg);
          this.closePage();
          this.pageSelect();
        });
      } else if (this.pageIndex == 2) {
        remove(this.user.id).then((res) => {
          Message.success(res.msg);
          this.closePage();
          this.pageSelect();
        });
      }
    },
    getRoleName(code) {
      let role = this.roleList.find((role) => {
        return role.code === code;
      });
      return role ? role.name : code;
    },
    changePage(page) {
      this.page.current = page;
      this.pageSelect();
    },
    changePageSize(size) {
      this.page.size = size;
    },
  },
};
</script>

<style scoped lang="less">
#user-view {
  height: 100%;
  width: 100%;
  display: flex;
  flex-direction: column;
  position: relative;

  .top {
    height: 2.4rem;
    display: flex;
    flex-direction: row;

    .operation {
      width: 20%;
      display: flex;
      flex-direction: row;
      align-items: center;

      .button {
        font-size: 1rem;
        width: 5rem;
        height: 2rem;
        display: flex;
        flex-direction: row;
        justify-content: center;
        align-items: center;
        border: solid 1px #999999;

        .button-icon {
          font-size: 1.4rem;
        }

        &:hover {
          cursor: pointer;
          border: solid 1px #1e90ff;
          color: #1e90ff;
        }
      }
    }

    .search {
      flex: 1;
      width: 100%;
      display: flex;
      flex-direction: row;
      justify-content: flex-end;
      align-items: center;

      .search-item {
        display: flex;
        flex-direction: row;
        align-items: center;
        margin-right: 1rem;

        .item-name {
          min-width: 4rem;
        }
      }
    }
  }

  .content {
    margin-top: 0.5rem;
    flex: 1;
    width: 100%;
    display: flex;
    flex-direction: column;

    .table-title {
      //padding-right: 1rem;
      height: 2.6rem;
      display: flex;
      flex-direction: row;
      align-items: center;
      width: 100%;
      background: #42b983;

      .title-item {
        flex: 1;
        min-width: 4rem;
        text-align: left;
        padding-left: 1rem;

        &.id {
          max-width: 6rem;
        }

        &.status {
          min-width: 6rem;
          max-width: 10rem;
        }

        &.button {
          min-width: 10rem;
          max-width: 16rem;
        }
      }
    }

    .table-content-container {
      width: 100%;
      flex: 1;
      position: relative;
      overflow-y: hidden;

      ::-webkit-scrollbar {
        display: none;
      }

      .table-content {
        position: absolute;
        overflow-y: auto;
        width: 100%;
        height: 100%;
        display: flex;
        flex-direction: column;

        .line {
          height: 2.6rem;
          line-height: 2.6rem;
          width: 100%;
          display: flex;
          flex-direction: row;
          background: #ffffff;
          margin-top: 0.2rem;

          .cell {
            flex: 1;
            text-align: left;
            padding-left: 1rem;

            &.id {
              max-width: 6rem;
            }

            &.roles {
              display: flex;
              flex-direction: row;
              //justify-content: center;
              align-items: center;

              .role {
                height: 1.6rem;
                line-height: 1.6rem;
                margin: 0 0.1rem;
                padding: 0 0.2rem;
                border: solid 1px #aaaaaa;
                border-radius: 0.2rem;
              }
            }

            &.status {
              min-width: 6rem;
              max-width: 10rem;
            }

            &.buttons {
              min-width: 10rem;
              max-width: 16rem;
              display: flex;
              flex-direction: row;
              //justify-content: center;
              align-items: center;

              .button {
                width: 3rem;

                &:hover {
                  cursor: pointer;
                  color: blueviolet;
                }

                &.remove {
                  color: crimson;
                }
              }
            }
          }
        }
      }
    }
  }

  .footer {
    border-top: dashed 1px #888888;
    height: 4rem;
    //background: bisque;
    display: flex;
    flex-direction: row;
    justify-content: center;
    align-items: center;
  }

  .more-page {
    width: 100%;
    height: 100%;
    position: fixed;
    top: 0;
    left: 0;
    background: rgba(100, 100, 100, 0.8);
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;

    .page {
      min-width: 10rem;
      max-height: 90%;
      background: #f1f1f1;

      .page-top {
        display: flex;
        flex-direction: row;
        border-bottom: dashed 1px #666666;
        height: 3rem;

        .top-left {
          flex: 1;
          display: flex;
          flex-direction: row;
          justify-content: flex-start;
          align-items: center;
          background: #f1f1f1;
          padding-left: 1rem;
          font-size: 1rem;
          font-weight: bold;
          color: #2d8cf0;
        }

        .button {
          width: 3rem;
          display: flex;
          flex-direction: row;
          justify-content: center;
          align-items: center;
          font-size: 2rem;
          color: #222222;

          &:hover {
            cursor: pointer;
            color: blueviolet;
          }
        }
      }

      .page-content {
        padding: 1rem 1rem;
      }

      .page-footer {
        height: 4rem;
        display: flex;
        flex-direction: row;
        justify-content: center;
        align-items: center;
        border-top: dashed 1px #666666;

        .button {
          font-size: 1rem;
          margin: 0rem 1rem;
          width: 5rem;
          height: 2rem;
          line-height: 2rem;
          border: 1px solid #2d8cf0;
          color: #2d8cf0;

          &:hover {
            cursor: pointer;
            color: #722ed1;
            border: 1px solid #722ed1;
          }
        }
      }
    }
  }
}
</style>
